package com.wzgl.core.model;



import java.util.List;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.wzgl.core.base.Table;
import com.wzgl.core.baseModel.BaseUser;
import com.wzgl.core.utils.StringUtils;

/***
 * 用户表
 * @author Administrator
 *
 */
@Table(tableName = "tusers", primaryKey = "fid")
public class Tusers extends BaseUser<Tusers> {
	private Log log=Log.getLog(Tusers.class);
	private static final long serialVersionUID = -1L;
	public static final Tusers dao = new Tusers();
	
	public List<Tusers> getAll(){
		return dao.find("select * from Tusers");
	}
	
	public Tusers getTusersByOpenId(String openId){
		return dao.findFirstByCache("tenMinute", "getTusersByOpenId"+openId,"select * from Tusers where openId=?",openId);
	}
	public Tusers findById(final String userId) {
		return dao.getCache(userId, new IDataLoader() {
			@Override
			public Object load() {
				return dao.doFindFirst("fid = ?", userId);
			}
		});
	}
	/**
	 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<Tusers> paginate(int pageNumber, int pageSize,String fname) {
		if(StringUtils.isNotBlank(fname)){
			return paginate(pageNumber, pageSize, "select *", "from Tusers where fnickname = ? order by fctime asc",fname);
		}
		return paginate(pageNumber, pageSize, "select *", "from Tusers order by fctime asc");
	}
	
	public Tusers findByEmail(final String email){
		return dao.getCache(email, new IDataLoader() {
			@Override
			public Object load() {
				return dao.doFindFirst("faccount = ?", email);
			}
		});
	}
	
	public Tusers findByTel(final String tel){
		return dao.getCache(tel, new IDataLoader() {
			@Override
			public Object load() {
				return dao.doFindFirst("fmobile = ?", tel);
			}
		});
//		return this.findFirst("select * from Tusers where fmobile=?", tel);
	}
//	public Tusers findByOpenId(String openId){
//		return this.findFirst("select * from Tusers where fopenid=?", openId);
//	}
//	public Tusers findKeyId(String fid){
//		return this.findFirst("select * from Tusers where fid=?", fid);
//	}
//	public boolean addjifen(String openId,int jifen){
//		Tusers Tusers = getTusersByOpenId(openId);
//		if (null!=Tusers) {
//			Tusers.set("level", Tusers.getInt("level")+jifen);
//		}
//		return Tusers.update();
//		
//	}
	
	/**
	 * 从缓存中加载用户信息
	 * @param userId
	 * @return
	 */
	public Tusers findInSession(String userId) {
		// 传入的userId为空直接返回null
		if (StrKit.isBlank(userId)) {
			return null;
		}
		return loadInSession(userId);
	}
	
	/**
	 * 根据map参数查找
	 * @param paras
	 * @return 
	 */
//	public List<Tusers> findByMap(Map<String, Object> paras) {
//		StringBuilder sql = new StringBuilder("select * from Tusers ");
//		if (paras.containsKey("order")) {
//			sql.append(" ORDER BY ");
//			sql.append(paras.get("order"));
//			sql.append(" ");
//		}
//		if (paras.containsKey("limit")) {
//			sql.append(" LIMIT ");
//			sql.append(paras.get("limit"));
//		}
//		return this.findByCache("tenMinute", "Tusers", sql.toString());
//	}

	
//	private static final String USER_CACHE_NAME = "session";

	/**
	 * 从缓存中加载用户信息
	 * @param userId
	 * @return
	 */
	public Tusers loadInSession(final String userId) {
		return CacheKit.get(CACHE_NAME, userId, new IDataLoader() {

			@Override
			public Object load() {
				return findById(userId);
			}
		});
	}
}
